<template>
    <div class="photo-con">
      <!-- 顶部滑动条区域 -->
      <div id="slider" class="mui-slider">
        <div id="sliderSegmentedControl"
          class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted">
          <div class="mui-scroll">
            <a :class="['mui-control-item', cateId == item.id ? 'mui-active': '']" v-for="item in cates" :key="item.id"
              @tap="getPhotoListByCateId(item.id)">
              {{item.title}}
            </a>
          </div>
        </div>
      </div>
  
      <!-- 图片列表区域 -->
      <div v-if="isLoading" class="loading">加载中...</div>
      <div v-else-if="error" class="error">{{ error }}</div>
      <ul v-else class="photo-list">
        <router-link v-for="item in list" :key="item.id" :to="{ name: 'photo_show', params: { id: item.id }}" tag="li">
          <img v-lazy="item.img_url" alt="图片">
          <div class="info">
            <h1 class="info-title">{{ item.title }}</h1>
            <div class="info-body">{{ item.zhaiyao }}</div>
          </div>
        </router-link>
      </ul>
    </div>
  </template>
  
  <script>
  import mui from '../../lib/mui/js/mui.min.js';
  
  export default {
    data() {
      return {
        cates: [], // 所有分类的列表数组
        list: [], // 图片列表的数组
        cateId: 0, // 当前分类ID
        isLoading: true, // 加载状态
        error: null, // 错误信息
      };
    },
    created() {
      this.getAllCategory();
      this.getPhotoListByCateId(0);
    },
    mounted() {
      // 初始化scroll滑动控件
      mui('.mui-scroll-wrapper').scroll({
        deceleration: 0.0005, // flick 减速系数
      });
    },
    methods: {
      // 获取图片的分类
      async getAllCategory() {
        try {
          const result = await this.$http.get('photo/getimgcategory');
          if (result.data.status === 0) {
            result.data.message.unshift({ title: '全部', id: 0 });
            this.cates = result.data.message;
          } else {
            this.error = '加载分类失败';
          }
        } catch (err) {
          this.error = '网络错误，请稍后重试';
        }
      },
      // 根据分类ID获取图片列表
      async getPhotoListByCateId(cateId) {
        this.cateId = cateId;
        this.isLoading = true;
        this.error = null;
  
        try {
          const result = await this.$http.get('photo/getimages/', { params: { id: cateId } });
          if (result.data.status === 0) {
            this.list = result.data.message;
          } else {
            this.error = '加载图片列表失败';
          }
        } catch (err) {
          this.error = '网络错误，请稍后重试';
        } finally {
          this.isLoading = false;
        }
      },
    },
  };
  </script>
  
  <style lang="scss">
  * {
    touch-action: pan-y; /* 去除警告 */
  }
  
  .photo-con {
    .loading,
    .error {
      text-align: center;
      font-size: 16px;
      color: #666;
      margin-top: 20px;
    }
  
    .error {
      color: red;
    }
  
    .photo-list {
      list-style: none;
      margin: 0;
      padding: 10px;
      padding-bottom: 0;
  
      li {
        background-color: #ccc;
        text-align: center;
        margin-bottom: 5px;
        box-shadow: 0 0 9px #999;
        position: relative;
  
        img {
          width: 100%;
          vertical-align: middle;
        }
  
        img[lazy="loading"] {
          width: 40px;
          height: 300px;
          margin: auto;
        }
  
        .info {
          color: white;
          text-align: left;
          position: absolute;
          bottom: 0;
          background-color: rgba(0, 0, 0, 0.4);
          max-height: 84px;
  
          .info-title {
            font-size: 14px;
          }
  
          .info-body {
            font-size: 13px;
          }
        }
      }
    }
  }
  </style>